package org.spbsu.timetable;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.evoj.GenePool;
import net.sourceforge.evoj.core.DefaultPoolFactory;
import net.sourceforge.evoj.handlers.DefaultHandler;

import org.apache.log4j.Logger;
import org.spbsu.timetable.example.Rating;
import org.spbsu.timetable.example.Solution;
import org.spbsu.timetable.model.Lesson;
import org.spbsu.timetable.model.Univercity;

/**
 * Main class. See details here: http://evoj-frmw.appspot.com/tutorial.html
 */
public class Main {
	private static final Logger LOG = Logger.getLogger(Main.class);

	public static void main(String[] args) {
		Univercity univercity = init();

		for (Lesson occupation : univercity.getOccupations()) {
			Integer audienceQnt = univercity.getAudienceQnt();
			Integer teacherPairsQnt = univercity.getTeacherPairsQnt(occupation
					.getTeacher());
			Integer groupPairsQnt = univercity.getStudentPairsQnt(occupation
					.getGroup());
			occupation.setLiberty((double) audienceQnt / teacherPairsQnt
					/ groupPairsQnt);
		}
		
		univercity.fillTimetable();

//		for (Pair occupation : univercity.getOccupations()) {
//			LOG.info(occupation);
//		}
		
		LOG.info("\n" + univercity.getTimetable().toString());
	}

	public static Univercity init() {
		List<Lesson> occupations = new ArrayList<>();

		occupations.add(new Lesson("345", "Logic", "Ivanov"));
		occupations.add(new Lesson("345", "Logic", "Ivanov"));
		occupations.add(new Lesson("345", "Logic", "Ivanov"));
		occupations.add(new Lesson("161", "Physical culture", "Petrov"));
		occupations.add(new Lesson("251", "Informatics", "Sidorov"));
		occupations.add(new Lesson("251", "Informatics", "Sidorov"));
		occupations.add(new Lesson("455", "Music", "Aguzarova"));

		return new Univercity(5, 6, 100, occupations);
	}

	public static void runSample() {
		LOG.info("Hello World!");
		DefaultPoolFactory pf = new DefaultPoolFactory();
		GenePool<Solution> pool = pf.createPool(200, Solution.class, null);
		DefaultHandler handler = new DefaultHandler(new Rating(), null, null,
				null);

		handler.iterate(pool, 200);
		Solution solution = pool.getBestSolution();
		LOG.info("(" + solution.getX() + ", " + solution.getY() + ")");
		LOG.info("End.");
	}

	// public static Integer
}
